// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.chat.defaultChats;

import java.nio.ByteBuffer;
import zombie.Lua.LuaManager;
import zombie.characters.IsoPlayer;
import zombie.chat.ChatBase;
import zombie.chat.ChatMessage;
import zombie.chat.ChatSettings;
import zombie.chat.ChatTab;
import zombie.chat.ChatUtility;
import zombie.core.Color;
import zombie.core.Translator;
import zombie.core.network.ByteBufferWriter;
import zombie.debug.DebugLog;
import zombie.network.GameServer;
import zombie.network.chat.ChatType;

public class GeneralChat extends ChatBase {
    private boolean discordEnabled = false;
    private final Color discordMessageColor = new Color(114, 137, 218);

    public GeneralChat(ByteBuffer bb, ChatTab tab, IsoPlayer owner) {
        super(bb, ChatType.general, tab, owner);
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }
    }

    public GeneralChat(int id, ChatTab tab, boolean _discordEnabled) {
        super(id, ChatType.general, tab);
        this.discordEnabled = _discordEnabled;
        if (!this.isCustomSettings()) {
            this.setSettings(getDefaultSettings());
        }
    }

    public GeneralChat() {
        super(ChatType.general);
    }

    public static ChatSettings getDefaultSettings() {
        ChatSettings chatSettings = new ChatSettings();
        chatSettings.setBold(true);
        chatSettings.setFontColor(new Color(255, 165, 0));
        chatSettings.setShowAuthor(true);
        chatSettings.setShowChatTitle(true);
        chatSettings.setShowTimestamp(true);
        chatSettings.setUnique(true);
        chatSettings.setAllowColors(true);
        chatSettings.setAllowFonts(true);
        chatSettings.setAllowBBcode(true);
        return chatSettings;
    }

    @Override
    public void sendMessageToChatMembers(ChatMessage msg) {
        if (this.discordEnabled) {
            IsoPlayer isoPlayer = ChatUtility.findPlayer(msg.getAuthor());
            if (msg.isFromDiscord()) {
                for (short short0 : this.members) {
                    this.sendMessageToPlayer(short0, msg);
                }
            } else {
                GameServer.discordBot.sendMessage(msg.getAuthor(), msg.getText());

                for (short short1 : this.members) {
                    if (isoPlayer == null || isoPlayer.getOnlineID() != short1) {
                        this.sendMessageToPlayer(short1, msg);
                    }
                }
            }
        } else {
            super.sendMessageToChatMembers(msg);
        }

        DebugLog.log("New message '" + msg + "' was sent members of chat '" + this.getID() + "'");
    }

    public void sendToDiscordGeneralChatDisabled() {
        GameServer.discordBot.sendMessage("Server", Translator.getText("UI_chat_general_chat_disabled"));
    }

    @Override
    public String getMessagePrefix(ChatMessage msg) {
        StringBuilder stringBuilder = new StringBuilder();
        if (msg.isFromDiscord()) {
            stringBuilder.append(this.getColorTag(this.discordMessageColor));
        } else {
            stringBuilder.append(this.getColorTag());
        }

        stringBuilder.append(" ").append(this.getFontSizeTag()).append(" ");
        if (this.isShowTimestamp()) {
            stringBuilder.append("[").append(LuaManager.getHourMinuteJava()).append("]");
        }

        if (this.isShowTitle()) {
            stringBuilder.append("[").append(this.getTitle()).append("]");
        }

        if (this.isShowAuthor()) {
            stringBuilder.append("[").append(msg.getAuthor()).append("]");
        }

        stringBuilder.append(": ");
        return stringBuilder.toString();
    }

    @Override
    public void packMessage(ByteBufferWriter b, ChatMessage msg) {
        super.packMessage(b, msg);
        b.putBoolean(msg.isFromDiscord());
    }

    @Override
    public ChatMessage unpackMessage(ByteBuffer bb) {
        ChatMessage chatMessage = super.unpackMessage(bb);
        if (bb.get() == 1) {
            chatMessage.makeFromDiscord();
        }

        return chatMessage;
    }
}
